#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       Translator.py 
# 说明：        目的、作用说明
#
# 版本：        0.01
# 作者：        xiao
#
# 创建时间：    2011-11-19 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import CollectBase as cb
from pyquery import PyQuery as pq
import os
import time
import random
import re

## bing的翻译接口
#
class TranslatorCall(cb.CollectBase):

	## 方法注释
	#
	#  @param
	#  @return
	#
	def parseData(self, txt, resp, urlDict):

		if os.path.isfile("num_q"):
			num_q = int(open("num_q").read().strip()) + 1

			if num_q > random.randint(20, 30):
				time.sleep(random.randint(30, 80))

			open("num_q", "w").write(str(num_q))
		else:
			open("num_q", "w").write("1")

		tmphtml = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>%s</title>
	</head>
	<body>
		<h1>%s</h1>
		<div>%s</div>
	</body>
</html>"""

		d = pq(txt)
		title = d("h1").text()
		htmltxt = d("#article-content").html()
		if htmltxt is not None and htmltxt != "":
			re_a = re.compile('</?a[^>]*>', re.I)
			htmltxt = re_a.sub('', htmltxt)
			try:
				open("d/%s.html" % title, "w").write(tmphtml % (title, title, htmltxt))
			except:
				print("1")
	#------------------------------------------ End def parseData()
#------------------------------------------------------ End class Translator()

if __name__ == "__main__":
	ud = []
	for url in open("d.txt").readlines():
		ud.append({"url":url.strip()})

	TranslatorCall(ud)
